import time

from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.page_load_strategy = "normal"  # 等待页面完全加载
# 创建浏览器驱动对象
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(5)
action = ActionChains(driver)
url = r"https://y.qq.com/"
driver.get(url)

driver.find_element(By.LINK_TEXT, "登录").click()
iframe = driver.find_element(By.XPATH, "//iframe")
driver.switch_to.frame(iframe)

iframe = driver.find_element(By.XPATH, "//iframe")
driver.switch_to.frame(iframe)
time.sleep(1)
driver.find_element(By.XPATH, "//*[@id='qlogin_list']/a").click()
time.sleep(1)
driver.switch_to.default_content()
driver.switch_to.default_content()
try:
    a = driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/div/div[2]/span/a")
    action.move_to_element(a).click().perform()
    driver.find_element(By.LINK_TEXT, "退出登录").click()
except NoSuchElementException as e:
    print(e.msg)

input("按任意键关闭")
# # 关闭驱动对象
driver.close()
driver.quit()
print("结束")
